package com.tvunetworks.center.device.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
/**
 * @program: tvucc-aggregator
 * @author lebronchen
 * @create: 2020-02-19 12:46
 **/
@Slf4j
public class HttpClientUtils {
    /**
     *
     * @param url url
     * @return String
     */
    public static String  doGetNoParam(String url) {

        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        // 创建Get请求
        HttpGet httpGet = new HttpGet(url);

        // 响应模型
        CloseableHttpResponse response = null;
        String result = "";
        try {
            // 由客户端执行(发送)Get请求
            response = httpClient.execute(httpGet);
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();
            System.out.println("响应状态为:" + response.getStatusLine());
            if (responseEntity != null) {
                result = EntityUtils.toString(responseEntity,"UTF-8");
                log.error("===doGetNoParam result=="+result);
            }
        } catch (ClientProtocolException e) {
            log.error("msg:{},e:{}",e.getMessage(),e);
        } catch (ParseException e) {
            log.error("msg:{},e:{}",e.getMessage(),e);
        } catch (IOException e) {
            log.error("msg:{},e:{}",e.getMessage(),e);
        } finally {
            try {
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                log.error("msg:{},e:{}",e.getMessage(),e);
            }
        }
        return result;
    }

    /**
     *
     * @param url url
     * @param jsonString jsonString
     * @return String
     */
    public static String doPostByJsonParam(String url,String jsonString) {

        CloseableHttpClient httpClient = HttpClientBuilder.create().build();

        // 创建Post请求
        HttpPost httpPost = new HttpPost(url);


        StringEntity entity = new StringEntity(jsonString, "UTF-8");

        // post请求是将参数放在请求体里面传过去的;这里将entity放入post请求体中
        httpPost.setEntity(entity);

        httpPost.setHeader("Content-Type", "application/json;charset=utf8");
        String result = "";
        // 响应模型
        CloseableHttpResponse response = null;
        try {
            // 由客户端执行(发送)Post请求
            response = httpClient.execute(httpPost);
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();

//            System.out.println("响应状态为:" + response.getStatusLine());
            if (responseEntity != null) {
                result = EntityUtils.toString(responseEntity,"UTF-8");
                log.error("===doPostByJsonParam result==="+result);
            }
        } catch (ClientProtocolException e) {
            log.error("msg:{},e:{}",e.getMessage(),e);
        } catch (ParseException e) {
            log.error("msg:{},e:{}",e.getMessage(),e);
        } catch (IOException e) {
            log.error("msg:{},e:{}",e.getMessage(),e);
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (response != null) {
                    response.close();
                }
            } catch (IOException e) {
                log.error("msg:{},e:{}",e.getMessage(),e);
            }
        }
        return result;
    }


}
